# -*- coding:GBK -*- 
from ctypes import *
import win32com.client,time,win32api
import random

PUL = POINTER(c_ulong)

class KeyBdInput(Structure):
        _fields_ = [("wVk", c_ushort),("wScan", c_ushort),("dwFlags", c_ulong),("time", c_ulong),("dwExtraInfo", PUL)]

class HardwareInput(Structure):
        _fields_ = [("uMsg", c_ulong),("wParamL", c_short),("wParamH", c_ushort)]

class MouseInput(Structure):
        _fields_ = [("dx", c_long),("dy", c_long),("mouseData", c_ulong),("dwFlags", c_ulong),("time",c_ulong),("dwExtraInfo", PUL)]

class Input_I(Union):
        _fields_ = [("ki", KeyBdInput),("mi", MouseInput),("hi", HardwareInput)]

class Input(Structure):
        _fields_ = [("type", c_ulong),("ii", Input_I)]

class POINT(Structure):
        _fields_ = [("x", c_ulong),("y", c_ulong)]
        
def get_mpos():
        orig = POINT()
        windll.user32.GetCursorPos(byref(orig))
        return int(orig.x), int(orig.y)
#</Get Pos>

#<Set Pos>
def set_mpos(pos):
        x,y = pos
        windll.user32.SetCursorPos(x, y)
#</Set Pos>

#<move and click>
def move_click(pos, move_back = False):
        origx, origy = get_mpos()
        set_mpos(pos)
        FInputs = Input * 2
        extra = c_ulong(0)
        ii_ = Input_I()
        ii_.mi = MouseInput( 0, 0, 0, 2, 0, pointer(extra) )
        ii2_ = Input_I()
        ii2_.mi = MouseInput( 0, 0, 0, 4, 0, pointer(extra) )
        x = FInputs( ( 0, ii_ ), ( 0, ii2_ ) )
        windll.user32.SendInput(2, pointer(x), sizeof(x[0]))
        if move_back:
                set_mpos((origx, origy))
        return origx, origy
    

if __name__ == '__main__':
        time.sleep(3)
        origx, origy = get_mpos()
        move_click((origx, origy))
        move_click((origx, origy))
        origx, origy = get_mpos()
        print 'Orig Pos:', origx, origy
        shell = win32com.client.Dispatch("WScript.Shell")
        win32api.Sleep(300)
        j=0
        while j<10:
            i = random.randint(0,2)
            if i==0:
                shell.SendKeys('hello world!', 0)
            elif i==1:
                shell.SendKeys('how are you!', 0)
            else:
                shell.SendKeys('how do you do!', 0)
            '''win32api.Sleep(200)
            shell.SendKeys("e", 0)
            win32api.Sleep(200)
            shell.SendKeys("l", 0)
            win32api.Sleep(200)
            shell.SendKeys("l", 0)
            win32api.Sleep(200)
            shell.SendKeys("o", 0)'''
            win32api.Sleep(2000)
            #shell.SendKeys("^~",0)#ctrl+enter
            shell.SendKeys("~",0)
            j=j+1